summation :: Int -> Int
summation n = n * (n + 1) `div` 2

sumMods :: Int -> Int -> Int
sumMods n m = nQm * sumOfMods + (summation nRm)
    where nQm = n `quot` m
          nRm = n `rem` m
          sumOfMods = summation (m - 1)  

main = do
    c <- getContents
    let ls = map words $ tail $ lines c
        ns = map (\(x1:x2:[]) -> sumMods (read x1) (read x2)) ls
    putStr $ unlines $ map show ns 